const { connectDB, closeDB } = require('../mongoose/database');
const User = require('../data/userData');
module.exports = {
  async adds (req, res) {
    try {
      //打开数据库
      await connectDB()
      const newUser = await User.create({
        id: new Date().getTime().toString(),
        name: 'zhangshan',
        age: 23,
        email: '104984515@qq.com',
      });
      closeDB()
      res.status(200).json(newUser);
      // res.send({ code: 200, message: 'okadd' });

    } catch (err) {
      console.log(err);
      res.status(500).json({ error: err });

    }



  },
  deleteByid (req, res) {
    res.send({ code: 200, message: 'okdelete' });
  },
  async updateByid (req, res) {
    try {
      //打开数据库
      await connectDB()
      const user = await User.update('6716882b0d067d4d39609102', { name: 'daipu' });
      closeDB()
      res.status(200).json(user);
    } catch (err) {
      console.log(err);
      res.status(500).json({ error: err });
    }



    res.send({ code: 200, message: 'okupdate' });
  },
  async gets (req, res) {

    try {
      //打开数据库
      await connectDB()
      const users = await User.find();
      res.status(200).json(users);
      closeDB()
    } catch (err) {
      console.log(err);
      res.send({ code: 500, message: err });

    }
  }
}